package com.shinhye.demoproject;

import android.content.ClipData;
import android.graphics.Canvas;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DragDemoActivity extends AppCompatActivity {

    private LinearLayout itemgroup;
    private TextView textview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo1);

        itemgroup = (LinearLayout) findViewById(R.id.itemgroup);
        textview = (TextView) findViewById(R.id.textview);
        textview.setMovementMethod(LinkMovementMethod.getInstance());

        textview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                dragItem(v);
                return true;
            }
        });
        itemgroup.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                switch (event.getAction()) {
                    case DragEvent.ACTION_DROP:
                        TextView view = generateTextView();
                        view.setText(textview.getText());
                        itemgroup.addView(view);
                        itemgroup.setBackgroundColor(0x66DD4400);
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        itemgroup.setBackgroundColor(0x99DD4400);
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        itemgroup.setBackgroundColor(0x66DD4400);
                        break;
                }
                return true;
            }
        });

        setTextViewBg(textview);
    }

    private void dragItem(View v) {
        ClipData data = ClipData.newPlainText("dot", "Dot : " + v.toString());
        v.startDrag(data, new MyShadowBuilder(v), v, 0);
    }

    private TextView generateTextView() {
        TextView textview = (TextView) View.inflate(this, R.layout.item_a_textview, null);
        setTextViewBg(textview);
        textview.setMovementMethod(LinkMovementMethod.getInstance());
        textview.setLayoutParams(new ViewGroup.LayoutParams(150 * 3 / 2, ViewGroup.LayoutParams.WRAP_CONTENT));
        textview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                itemgroup.removeView(v);
                return true;
            }
        });
        return textview;
    }

    private void setTextViewBg(TextView textview) {
        //设置textview的background
        float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
        RectF   inset = new RectF(6, 6, 6, 6);
        float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };
        ShapeDrawable drawable = new ShapeDrawable(new RoundRectShape(outerR, inset, innerR));
        PathEffect pe = new DiscretePathEffect(10, 4);
        PathEffect pe2 = new CornerPathEffect(4);
        drawable.getPaint().setPathEffect(new ComposePathEffect(pe2, pe));
        textview.setBackgroundDrawable(drawable);
    }

    class MyShadowBuilder extends View.DragShadowBuilder {
        public MyShadowBuilder(View v) {
            super(v);
        }

        @Override
        public void onDrawShadow(Canvas canvas) {
//            ImageView imageView = new ImageView(DragDemoActivity.this);
//            imageView.setBackgroundColor(Color.GRAY);
//            imageView.setImageResource(R.drawable.ic_menu_gallery);
//            imageView.draw(canvas);

            super.onDrawShadow(canvas);
        }

        @Override
        public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
            super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
//            shadowSize.set(100, 100);
//            shadowTouchPoint.set(90, 90);
        }
    }
}
